# 1.导包
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
# 2.打开浏览器（创建浏览器驱动对象）
path = r"C:\Program Files\Python311\chromedriver.exe"  # 定义驱动路径
ser = Service(executable_path=path)  # 实例化Chrome浏览器服务驱动
driver = webdriver.Chrome(service=ser)  # 打开Chrome浏览器
# driver = webdriver.Edge()
# 3.输入网址
driver.get("https://www.baidu.com/")
# 4.页面操作
driver.find_element(By.XPATH, '//*[@id="s-top-left"]/a[1]').click()
handles = driver.window_handles
print(handles)
driver.switch_to.window(handles[1])
time.sleep(1)
driver.close()
# 5.等待2秒
time.sleep(2)
# 6.退出浏览器
driver.quit()